/**
 * This class represents a custom rss feed parser which is used to parse
 * rss feeds for all rss feeds except the events calendar module in the 
 * iWestern application. It is a subclass of RssFeedParser and uses 
 * methods / attributes from this class when parsing the rss feed.
 * @author Chirag Mistry
 */

package ca.uwo.android.rss;

import java.io.InputStream;

import org.xml.sax.Attributes;

import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.sax.StartElementListener;
import android.util.Log;
import android.util.Xml;

public class SaxRssParser extends RssFeedParser {
	
	/**
	 * Constructor for the rss feed parser
	 * @param rssFeedURL - the url of the rss feed to parse
	 */
	public SaxRssParser(String rssFeedURL) throws Exception{
		super(rssFeedURL);
	}
	 

	/**
	 * This method parses the rss feed and returns an rss feed object containing
	 * all the feed items read in from the source
	 * @return
	 */
	public RssFeed parse() throws Exception{
		
		try {
			final RssItem currentItem = new RssItem();	//represents current item being parsed
			RootElement root = new RootElement(RSS);	//define the root element of the rss feed document
			
			final RssFeed rssItems = new RssFeed();		//rss feed object to store all rss items
			
			Element channel = root.getChild(CHANNEL);	//get the channel element from root
			
			String rssFeedText;
			
			InputStream is = this.getInputStream();	//get the input stream to the rss feed
			rssFeedText = convertSpecialChars(is);	//convert special characters in feed	
			
			
			//get each (child) item element from the channel and store it in the rss feed object
			Element item = channel.getChild(ITEM);
			item.setEndElementListener(new EndElementListener() {
				public void end() {
					RssItem copiedItem = currentItem.copy();
					rssItems.add(copiedItem);
				}
			});
			
			//parse and store the title of the rss feed item
			item.getChild(TITLE).setEndTextElementListener(
					new EndTextElementListener() {
						public void end(String body) {
							currentItem.setTitle(body);
						}
					});
			
			//parse and store the link of the rss feed item
			item.getChild(LINK).setEndTextElementListener(
					new EndTextElementListener() {
						public void end(String body) {
							currentItem.setLink(body);
						}
					});
			
			//parse and store the description of the rss feed item
			item.getChild(DESCRIPTION).setEndTextElementListener(
					new EndTextElementListener() {
						public void end(String body) {
							if(body.indexOf("<img src") != -1){
								//removes <img src=""> tags from description (for mustang news)
								body = body.replaceAll("\\x3Cimg src{1}.*\\x2F\\x3E", "");
							}
							currentItem.setDescription(body);
						}
					});
			
			
			//parse and store the date of the rss feed item
			item.getChild(PUBDATE).setEndTextElementListener(
					new EndTextElementListener() {
						public void end(String body) {
							currentItem.setDate(body);
						}
					});
			
			
			// parse and store the image for Notable Alumni
			Element image = item.getChild(IMAGE);
			
			if(image != null){
			// just need the url, which is an attribute of this element
			image.setStartElementListener(new StartElementListener() {

				@Override
				public void start(Attributes attributes) {
					String type = attributes.getValue("type");
					String url = attributes.getValue("url");
					currentItem.setImage(url);
				}
			});
			}
				
			//parse the rss feed directly from the input stream using utf 8 encoding
			Xml.parse(rssFeedText, root.getContentHandler());

			return rssItems;
		} catch (Exception e) {
			
			
//			Log.e("!!!!Exception!!!!!", "Exception parsing!!! " + e.getMessage(), e);
			
			throw e;
		}
	}
}

